import json

from backend.common.webserviceEncryption import webserviceEncryption
import requests

"""
外部调用webservice测试 参考
"""
print(1)
path = "files/"
url = "http://192.168.16.12:6789/api/test/gzzcdZzEncrypt"  # 加密地址
headers = {
    'Content-Type': 'application/json'
}
id = 22
result = webserviceEncryption(path, id, url, headers)
print("==================")
print(result)
print("==================")
print(type(result))
print(result['resHead'])
print(11)


"""调用真正的webservice接口
需要替换参数：从上面的参数获取到并替换

"""

#参数
url = "http://192.168.16.12:6789/services/RFPServerForBankService?wsdl"

# payload固定的不需要修改
payload="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.gzzcd.credit.lyzd.com\">\r\n    <soapenv:Header/>\r\n    <soapenv:Body>\r\n        <web:serviceAndFile>\r\n            <data>\r\n                <![CDATA[<DOCUMENT><TITLE>9111000010112001XW</TITLE><HEAD>HEADREPLACE</HEAD><BODY>BODYREPLACE</BODY></DOCUMENT>]]>\r\n\r\n            </data>\r\n            <file></file>\r\n        </web:serviceAndFile>\r\n    </soapenv:Body>\r\n</soapenv:Envelope>"
#<HEAD>HEADREPLACE</HEAD><BODY>BODYREPLACE</BODY>
payload=payload.replace("HEADREPLACE",result['resHead'])
payload=payload.replace("BODYREPLACE",result['resBody'])
# 参数
headers = {
  'Content-Type': 'application/xml'
}

# 参数post
response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
#正常方法的返回

res=response.text
print("EEEEEEEEE")
# print(3)
#coding:utf-8
import xml.dom.minidom
resxml = xml.dom.minidom.parseString(res)
xml_pretty_str = resxml.toprettyxml()
print(xml_pretty_str)
print("FFFFFFFF")
print(type(xml_pretty_str))

xmlstr=str(xml_pretty_str)
xmlstr=xmlstr.replace('<?xml version="1.0" ?>','')
xmlstr=xmlstr.replace("<?xml version='1.0' ?>",'')




print(xmlstr)
print("GGGGGGGG")

# a=response.text
headstart=xmlstr.rfind("&lt;HEAD&gt;")
headend=xmlstr.rfind("/HEAD&gt")
substrhead=xmlstr[headstart:headend]
substrhead=substrhead.replace("&lt;HEAD&gt;",'')
decryptHead=substrhead.replace("&lt;",'')
print(decryptHead)#截取后的head



bodystart=xmlstr.rfind("&lt;BODY&gt;")
bodyend=xmlstr.rfind("/BODY&gt;")
substrbody=xmlstr[bodystart:bodyend]
substrbody=substrbody.replace("&lt;BODY&gt;",'')
decryptBody=substrbody.replace("&lt;",'')
print(decryptBody)#截取后的head
# BDzvrDmDMyEZDG2ynaTcBPHMRnmoJReIW7vVOgNIyxyof8p3OmGK1+yg2qVSsyJat5A/ung5dNgNp1DoksIqllsMokNYRTfAUIq24SkmsIy6wrohrNP+RwnA0vSLsecHQWEzYBrnLvhpxwtl72u20aHKmHqowD0dn+mHUlW2mC3Mbi3yQjULCbdpJSX+kx2Sd8vlVRLk0Vj3OVOWu+E0BQOuwOGqgdNWEHzNapKAYfhSCgNg/wmA2os8XAckK5YADRtKVNSr0pGtrmsKxQDIG6UbUhIVeIfvIbAuCiD0Rwu/G9Oxb+vzdh87Ze8E95uBrOJSndVeXEwSetrMkovexdPw5v9NWx5VKY3jjsKlvzkC2IGwqRckJhjFWjo1M1Lg7q7/0uPOESz/CYdMSnS3OANoqFQuMG+VgMS6xU7OZfQ1atH8GMe9Nkg14Rs1VBWNmD7ApzwlOUSgyIk=
# BBHUzImibZwfua5im9a3gG8UwN6cjRiDVRvCNjvzn3LuemHpXAsmrOvfEdRBFyQnmncqvmprWRxMz1d6jB1fKN5sgoxc41x2oIxLI+/fn8fZ7X1RsFgFn8ESx8Q4gDT8LMOnZ//UpLl7FUvQqandjgt+mzRP/csESwpBVXVaRZReYnPtXg==


"""
param = {"reqHead": decryptHead, "reqBody": decryptBody}

#
#
#
#
# """
# 读取解密数据
# """
# # with open("files/ceshi.txt", "r", encoding='utf-8') as f:
# #
# #     a = f.read()
# #     f.close()
# # print(a)
#
# print("aaaa")
#
# # xmltojson
#
# """解析xml，读取head及body
# 为解密做准备"""
# # parse是的xml解析器
# xml_parse = xmltodict.parse(xmlstr)
# print(xml_parse)
# # json库dumps()是将dict转化成json格式,loads()是将json转化成dict格式。
# # dumps()方法的ident=1,格式化json
# # json_str = json.dumps(xml_parse, indent=1)
# # print(json_str)
# print(xml_parse['soapenv:Envelope']['soapenv:Body']['web:serviceAndFile']['data'])
# data = xml_parse['soapenv:Envelope']['soapenv:Body']['web:serviceAndFile']['data']
#
# """
# 为了读取方便，需要二次将xml转dict，方便读取
# """
# json_data = xmltodict.parse(data)
# print(1111111111111)
# print(json_data)
# decryptHead = json_data['DOCUMENT']['HEAD']
# decryptBody = json_data['DOCUMENT']['BODY']
# print(json_data['DOCUMENT']['HEAD'])
# print(json_data['DOCUMENT']['BODY'])
# print(22222222)

"""
构造解密入参

"""
param = {"reqHead": decryptHead, "reqBody": decryptBody}
param=json.dumps(param)
url = "http://192.168.16.12:6789/api/test/gzzcdZzDecrypt"
headers = {
    'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=param)
resjiemi=response.text
print(resjiemi)
# {"returnCode":"000000","message":"成功","data":{"resHead":"<TRANSCODE>C1703</TRANSCODE><MSGID>28facd3a25f84852ae96428160d79eeb</MSGID><ORGNLMSGID>913400001489746613201904151555307438695</ORGNLMSGID><SESSIONNO></SESSIONNO><SENDDATE>20210325</SENDDATE><SENDTIME>105402</SENDTIME><ERRCODE>000000</ERRCODE><ERRMSG>成功</ERRMSG>","resBody":"<ERR_CODE>000000</ERR_CODE><ERR_MSG>成功</ERR_MSG>"}}
#

